/*
 * @Author: your name
 * @Date: 2024-05-29 11:52:34
 * @LastEditTime: 2024-05-29 12:06:37
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\135.分发糖果.cpp
 */
/*
 * @lc app=leetcode.cn id=135 lang=cpp
 *
 * [135] 分发糖果
 */

// @lc code=start
class Solution {
public:
    int candy(vector<int>& ratings) {
        vector<int> candyVec(ratings.size(), 1);
        // 从前往后
        for (int i = 1; i < candyVec.size(); i++)
        {
            /* code */
            if(ratings[i] > ratings[i-1]){
                candyVec[i] = candyVec[i-1]+1;
            }

        }
        // 从后往前
        for (int  i = ratings.size() - 2; i >=0; i--)
        {
            /* code */
            if(ratings[i] > ratings[i+1]){
                candyVec[i] = max(candyVec[i], candyVec[i+1]+1);
            }
        }
        // 统计结果
        int result = 0;
        for (int i = 0; i < candyVec.size(); i++) result += candyVec[i];
        return result;
        
        
    }
};
// @lc code=end

